package com.affectu.search.fetch;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

import com.affectu.search.fetch.thread.PaperThread;
import com.affectu.study.Paper;

/**
 * Oct 15, 2008
 * 
 * @author daniel nathan
 */
public class WitkeyTheory extends PaperThread {

	public WitkeyTheory() {
		super("http://www.witkey.com/witkey_theory.html", "/article/");// addition
																		// fecth
	}

	@Override
	public Paper spideSubPage(String url) {
		Paper paper = new Paper();
		paper.setLink(url);
		String name = "";
		String content = "";
		try {
			OrFilter allFilter = new OrFilter();
			allFilter.setPredicates(new NodeFilter[] {
					new NodeClassFilter(Div.class),
					new NodeClassFilter(TableTag.class) });
			NodeList nodeList = new Parser(url).parse(allFilter);
			for (Node node : nodeList.toNodeArray()) {

				String attributeValue;
				if (node instanceof Div) {
					Div divNode = (Div) node;
					attributeValue = divNode.getAttribute("class");
					if ("mleft_tle".equals(attributeValue)) {
						// ...得到新闻标题(纯文本)
						name = divNode.toPlainTextString();
						this.log(name);
						paper.setName(name);
					} else if ("margin:8px 0px 0px 0px; line-height:26px; font-size:14px; word-spacing:5px;"
							.equals(divNode.getAttribute("style"))) {
						// ...得到金额(纯文本)
						content = divNode.toHtml();
						this.log(content);
						paper.setContent(content);
					}

				} else if (node instanceof TableTag) {
					TableTag tableNode = (TableTag) node;
					attributeValue = tableNode.getAttribute("class");
					if ("taskinfotable".equals(attributeValue)) {

					}
				}

			}// end for
		} catch (Exception ex) {
			this.logAffectuException(ex, "抓取子页出错 :" + url);
		}
		return paper;
	}

}
